/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app;

import java.util.HashMap;
import java.util.Vector;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import lib.JButtonTable.ButtonEditor;
import lib.JButtonTable.ButtonModel;
import lib.JButtonTable.ButtonRenderer;
import lib.multiSpanCellTable.MultiSpanCellTable;

/**
 *
 * @author yoshi
 */
public class MyTable extends MultiSpanCellTable {

    private HashMap<Integer, Vector> map = new HashMap<Integer, Vector>();
    private HashMap<Integer, ButtonEditor> editorMap = new HashMap<Integer, ButtonEditor>();
    private HashMap<Integer, ButtonRenderer> rendererMap = new HashMap<Integer, ButtonRenderer>();
    private Vector vItuneColumns = new Vector();
    private Vector disabledSelectColumns = new Vector();
    private MyCellRenderer defaultRenderer = new MyCellRenderer(SwingConstants.LEFT);

    public MyTable() {
        super();
        this.setRowHeight(32);
    }
    
    public void refresh(){
        map.clear();
        editorMap.clear();
        rendererMap.clear();
        vItuneColumns.clear();
        disabledSelectColumns.clear();
    }

    public void makeButtonColumn(int index, ButtonEditor editor, ButtonRenderer renderer) {
        this.map.put(index, new Vector());
        this.editorMap.put(index, editor);
        this.rendererMap.put(index, renderer);
        this.getColumnModel().getColumn(index).setCellRenderer(renderer);
        this.getColumnModel().getColumn(index).setCellEditor(editor);
    }

    public void disableButtonCell(int column, ButtonModel model) {
        int rowCount = this.getRowCount();

        for (int row = 0; row < rowCount; row++) {
            if (model.equals(this.getValueAt(row, column))) {
                this.disableButtonCell(row, column);
            }
        }
    }
    
    public void setColumnWidth(int column, int width){
        TableColumn col =this.getColumnModel().getColumn(column);
        col.setPreferredWidth(width);
        col.setMaxWidth(width);
        col.setMinWidth(width);
        col.setWidth(width);
    }

    public void disableButtonCell(int row, int column) {
        Vector v = this.map.get(column);
        v.add(row);
    }
    
    public void disableSelectColumn(int column){
        disabledSelectColumns.add(column);
    }

    public void makeItuneColumn(Vector columns) {
        vItuneColumns = columns;
    }
    
    public void makeItuneColumn(int column){
        vItuneColumns.add(column);
    }
    
    public Vector getItuneColumns(){
        return vItuneColumns;
    }

    public Vector getDisabledSelectColumns() {
        return disabledSelectColumns;
    }

    @Override
    public TableCellRenderer getDefaultRenderer(Class<?> columnClass) {
        return defaultRenderer;
    }

    private int i = 0;

    @Override
    public boolean isCellEditable(int row, int column) {
        if (this.map.containsKey(column)) {
            Vector v = this.map.get(column);
            if (v.contains(row)) {
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}
